package com.juguo.charginganimation.View.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;

import com.juguo.charginganimation.Bean.PrivacyBean;
import com.juguo.charginganimation.Data.remote.RetrofitManager;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.utils.RomUtil;
import com.juguo.charginganimation.utils.RxUtils;
import com.juguo.charginganimation.utils.UITools;
import com.juguo.charginganimation.repository.PixabayService;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;

public class PrivacyActivity extends AppCompatActivity {
    private static final String TAG = "cimoGallery";
    private TextView tilite;
    private TextView tvqx;
    private WebView urlWebView;
    private CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy);
        UITools.makeStatusBarTransparent(this);
        UITools.setMIUI(this, true);
        tilite = findViewById(R.id.tv_title);
        tilite.setText("隐私政策");
        initViewAndData();
        onclick();
    }

    private void initViewAndData() {
        PixabayService service = RetrofitManager.getInstance()
                .getRetrofit()
                .create(PixabayService.class);
        mDisposable.add(service.getPrivacy()
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Consumer<PrivacyBean>() {
                    @Override
                    public void accept(PrivacyBean privacyBean) throws Exception {
                        Log.d(TAG, "accept: " + privacyBean);
                        urlWebView = PrivacyActivity.this.findViewById(R.id.webView);
                        if (RomUtil.isVivo()) {
                            //如果是vivo设备
                            urlWebView.loadDataWithBaseURL(null, privacyBean.getResult().getVivo(), "text/html", "UTF-8", null);
                        } else if (RomUtil.isEmui()) {
                            //如果是华为设备
                            urlWebView.loadDataWithBaseURL(null, privacyBean.getResult().getHuawei(), "text/html", "UTF-8", null);
                        }
                    }
                }, throwable -> Log.d(TAG, "loadMore: " + throwable)));

    }

    private void onclick() {
        tvqx = findViewById(R.id.tv_qx);
        // alt+ enter
        tvqx.setOnClickListener(v -> finish());
    }
}